<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
if (!defined("Tool_lib_include")){
    define("Tool_lib_include", 1);

/**
 * 產生分盤賠率的 File
 * /common/gambletype/GambleType?x_?y.inc.php
 * ?x：1 -> A盤， 2 -> B 盤， 3 -> C 盤
 * ?y：Lotto 序號
 * @return	成功傳回 true，否 false
 */
	function MakeGameGambleType(){
		global $_Conn, $$_Conn;
		$strSQL = "select LottoID, Bet_Type, ApointNum, GambleType, SunValue, MaxValue, MinValue from `bet_LottoOdds` where LottoID > 0 order by LottoID, GambleType, Bet_Type, ApointNum";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Make GambleType File Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		$arrGType = array();
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrGType[$Rows -> LottoID][$Rows -> GambleType][$Rows -> Bet_Type][$Rows -> ApointNum] = $Rows -> SunValue;
		}
		@mysql_free_result($RS);
		
		foreach($arrGType as $intLID => $arrV){
			if(!is_array($arrV))continue;
			$strJSCon = "var arrGLine = [];\n";

			foreach($arrV as $intGLine => $arrBTLine){
				if(!is_array($arrBTLine))continue;
				$strCon = "";
				$strJSCon .= "arrGLine[".$intGLine."] = [];\n";
				foreach($arrBTLine as $intBT => $arrANLine){
					$arrGV = array();
					foreach($arrANLine as $intAN => $intGLineValue){
						$arrGV[$intAN] = ($intGLineValue - $arrGType[$intLID][1][$intBT][$intAN]) / 2;
						$strCon .= "$"."_arrGambleType[".$intBT."][".$intAN."] = ".$arrGV[$intAN].";\n";
						$strCon .= "$"."_arrGambleTypeAllOdds[".$intBT."][".$intAN."] = ".$intGLineValue.";\n";
					}
					$strJSCon .= "arrGLine[".$intGLine."][".$intBT."] = [".implode(",", $arrGV)."];\n";
				}
				$strCon = "<"."?php \n".$strCon."?".">";
				$strFile = __Common_Path."/gambletype/GambleType".$intGLine."_".$intLID.".inc.php";
				if(!writeFile($strFile, $strCon, "w"))return false;
			}
			$strFile = __Common_Path."/gambletype/GambleType".$intLID.".js";
			if(!writeFile($strFile, $strJSCon, "w"))return false;
		}
		return true;
	}  //  MakeGameGambleType()  //


/**
 * 從 Lotto 分盤賠率轉到賽程分盤賠率
 * @param	arrGID 賽程序號，陣列型態 
 * @param	arrLID Lotto序號，陣列型態 
 * @return	成功傳回 true，否 false
 */
	function ChangeOddsLottoToGamble($arrGID, $arrLID){
		global $_ConnMaster;

		if(!is_array($arrGID) || !is_array($arrLID)){
			$GLOBALS["errormessage"] = "Gamble or Lotto not Array!";
			$GLOBALS["errorhidden"] = "\nError: arrGID = ".is_array($arrGID).", arrLID = ".is_array($arrLID)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}  //  if(is_array($arrGID))  //

		while(list($k,$v) = each($arrGID)){
			$strSQL = "select * from `bet_LeagueOdds` where LeagueID = '".$arrLID[$k]."'";
			if(!$RS = @PMA_mysql_query($strSQL, $_ConnMaster)){
				$GLOBALS["errormessage"] = "Lotto Odds To Gamble Error";
				$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
				@mysql_free_result($RS);
				return false;
			}

			while($Rows = mysql_fetch_object($RS)){
				$strSQL = "select ID from `bet_GambleOdds` where GambleID = '".$v."' and Bet_Type = '".$Rows -> Bet_Type."' and Gamble_Type = '".$Rows -> Gamble_Type."'";
				if(!$RSS = @PMA_mysql_query($strSQL, $_ConnMaster)){
					$GLOBALS["errormessage"] = "Lotto Odds To Gamble Error";
					$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
					@mysql_free_result($RS);
					return false;
				}

				if(PAM_mysql_num_rows($RSS) == 0){
					$strSQL = "insert into `bet_GambleOdds`(GambleID,Bet_Type,Gamble_Type,Odds,MaxValue,MinValue) values('".$v."','".$Rows -> Bet_Type."','".$Rows -> Gamble_Type."','".$Rows -> Odds."','".$Rows -> MaxValue."','".$Rows -> MinValue."')";
					if(!PMA_mysql_query($strSQL, $_ConnMaster)){
						$GLOBALS["errormessage"] = "Lotto Odds To Gamble Error";
						$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
						@mysql_free_result($RS);
						@mysql_free_result($RSS);
						return false;
					}
				}else{
					$Rowss = PMA_mysql_fetch_object($RSS);
					$strSQL = "update `bet_GambleOdds` set Odds = '".$Rows -> Odds."', MaxValue = '".$Rows -> MaxValue."', MinValue = '".$Rows -> MinValue."' where ID = ".$Rowss -> ID;
					if(!PMA_mysql_query($strSQL, $_ConnMaster)){
						$GLOBALS["errormessage"] = "Lotto Odds To Gamble Error";
						$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
						@mysql_free_result($RS);
						@mysql_free_result($RSS);
						return false;
					}
				} // if(PAM_mysql_num_rows($RSS) == 0)
			}  // while($Rows = mysql_fetch_object($RS))
		}  // while(list($k,$v) = each($arrGID))  //
		return true;
	}  // ChangeOddsLottoToGamble()  //


/**
 * 產生管理介面 NavBar
 * 輸出
 * NavBar?x_?y.php：管理介面 Navbar 參數檔
 * NavBarNo?x_?y.php：管理介面 Navbar 參數檔，不含即時注單功能
 * ?x：群組代號，2 -> 代理商，3 -> 總代理......依 bet_MemGroup 裡頭定義
 * ?y：語言版本，1 -> 繁體， 2 -> 英文， 3 -> 簡體
 * @return	成功回傳 true，否 false
 */
	function MakeAdminMenu(){
		global $_Conn, $$_Conn;
		$arrLanPoint = array("", "‧", "&nbsp;|&nbsp;", "ㄝ");

		//找出各群組的 program id
		$strSQL = "select ID, ProgramID from `bet_MemberGroup` where ID != 1";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Make Menu File Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		while($Rows = PMA_mysql_fetch_object($RS)){
			//依各群組 program id 找出其上層所有 program id
			if(empty($Rows -> ProgramID))$Rows -> ProgramID = "0";
			$strPID = QueryAllProgram($Rows -> ProgramID);
			//找出所有最上層 program
			$strSQL = "select * from `bet_Program` where ParentID = 0 and ID in (".$strPID.") order by SerialNO desc, ID";
			if(!$subRS = @PMA_mysql_query($strSQL, $$_Conn)){
				$GLOBALS["errormessage"] = "Make Menu File Error!!";
				$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
				return false;
			}

			$subRows = PMA_mysql_num_rows($subRS);
			for($j = 1; $j < sizeof($arrLanPoint); $j++){
				$strCon = "";
				$strConNoBetList = "";
				$strSubMenu = "";
				$kk = 1;
				$strSpace = ($j == 2)?"&nbsp;&nbsp;":"";
				for ($i = 0; $i < $subRows; $i++) {
					if (strlen(PMA_mysql_result($subRS, $i, "ProgramURL")) > 0) {
						$strTmpCon =  "<a href=\"".PMA_mysql_result($subRS,$i,"ProgramURL")."\" class=\"top_admin\" target=\"bottom\">".PMA_mysql_result($subRS, $i, "ProgramName_".$j)."</a>&nbsp;&nbsp;-&nbsp;&nbsp;\n";
					} else {
						$strTmpCon = "<a href=\"javascript:void(0)\" onClick=\"hideAll();popUp('elMenu".$kk."',event);return false\" class=\"top_admin\">".PMA_mysql_result($subRS, $i, "ProgramName_".$j)."</a>&nbsp;&nbsp;-&nbsp;&nbsp;\n";
						//產生下拉清單所需資料
						$intID = PMA_mysql_result($subRS, $i, "ID");
						$strTmp = "arMenu".$kk;
						$strSubMenu .= QuerySubMenu($strTmp, $intID, $strPID, $j);
						$kk++;
					}
					$strCon .= $strTmpCon;
					//代理商，沒有即時注單的版本，ps. 即時注單的序號是 3
					if($Rows -> ID == 2 && PMA_mysql_result($subRS, $i, "ID") != 3)$strConNoBetList .= $strTmpCon;
				}

				//寫入檔案
				$strFile = __Common_Path."/navbar/NavBar".$Rows -> ID."_".$j.".php";
				$strCon = "<"."?php \n\$strNavbar = \"".str_replace("\"", "\\\"", $strCon)."\";\n"."?".">";
				if(!writeFile($strFile, $strCon, "w"))return false;

				//代理商，沒有即時注單的版本
				if($Rows -> ID == 2){
					$strFile = __Common_Path."/navbar/NavBarNo".$Rows -> ID."_".$j.".php";
					$strCon = "<"."?php \n\$strNavbar = \"".str_replace("\"", "\\\"", $strConNoBetList)."\";\n"."?".">";
					if(!writeFile($strFile, $strCon, "w"))return false;
				}

				//寫入檔案
				$strFile = __Common_Path."/navbar/SubMenu".$Rows -> ID."_".$j.".php";
				$strCon = "<"."?php \n\$strSubMenu = \"".addslashes($strSubMenu)."\";\n"."?".">";
				if(!writeFile($strFile, $strCon, "w"))return false;
			}
		}
		return true;
	}  //  function MakeAdminMenu()  //

/**
 * 根據 program id 找出其上層所有 id
 * @param	strPID program id ，ex: 2,4,5,6,.......,n
 * @return	成功回傳 字串 ，ex: 2,4,5,6,.......,n
 */
	function QueryAllProgram($strPID){
		global $_Conn, $$_Conn;
		if(strlen($strPID) == 0)return "";
		$strSQL = "select ParentID from `bet_Program` where ID in (".$strPID.") Group by ParentID Order by SerialNO desc, ID";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Make Menu File Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn).$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
			if(PMA_mysql_num_rows($RS) > 0){
				while($Rows = PMA_mysql_fetch_object($RS)){
					$arrP[] = $Rows -> ParentID;
				}
				$strPID .= ",".QueryAllProgram(join(",", $arrP));
			}
			@mysql_free_result($RS);
		}
		return $strPID;
	}

/**
 * 產生 Menu 檔案所需文字，ps. 管理介面 NavBar 圖層式下拉清單用
 * @param	strThisName arMenuXX 名稱
 * @param	intParentID program 父層 ID
 * @param	strPID program id ，ex: 2,4,5,6,.......,n
 * @param	intLang 語言序號
 * @return	成功回傳 字串
 */
	function QuerySubMenu($strThisName, $intParentID, $strPID, $intLang) {
		global $_Conn, $$_Conn;
		//$tdwidth = 80;
		$intMenuTop = 70;
		$strThisMenu="";
		$strSubMenus="";
		$strSQL="select * from bet_Program where ID in (".$strPID.") and ParentID=" .$intParentID. " order by SerialNO desc, ID";

		if ($RS = @PMA_mysql_query($strSQL, $$_Conn)) {
			$i = 0;
			$intNum = PMA_mysql_num_rows($RS);
			if ($intNum > 0) {
				if (strpos($strThisName,"_")>0) {
					$strThisMenu = $strThisName . " = new Array(\n";
				} else {
					$strThisMenu = $strThisName . " = new Array(\n";
					$strThisMenu .= "\"\",\n";
					$strThisMenu .= "\"20\",\"".$intMenuTop."\",\n";
					$strThisMenu .= "\"black\",\"white\",\n";
					$strThisMenu .= "\"#c0c0c0\",\"#000080\",\n";
					$strThisMenu .= "\"black\",\"#eeeeee\"";
				}

				$k=1;
				while ($i < $intNum) {
					$strThisSubMenu = QuerySubMenu($strThisName."_".($i+1), PMA_mysql_result($RS, $i, "ID"), $strPID, $intLang);
					$strSubMenus .= $strThisSubMenu;

					if (strlen($strThisSubMenu) > 0) {
						if (strpos($strThisName, "_") == 0 || $i > 0) {
							$strThisMenu .= ",\n";
						}
						$strThisMenu .= "\"". PMA_mysql_result($RS, $i, "ProgramName_".$intLang). "\",\"\",1";
						$k++;
					} else {
						if (strpos($strThisName, "_") == 0 || $i > 0) {
							$strThisMenu .= ",\n";
						}
						$strThisMenu .= "\"". PMA_mysql_result($RS, $i, "ProgramName_".$intLang). "\",\"" . mysql_result($RS, $i, "ProgramURL")."\",0";
					}
					$i++;
				}
				$strThisMenu .= ");\n\n";
				$strThisMenu .= $strSubMenus;
			}
		}
		@mysql_free_result($RS);
		return $strThisMenu;
	}

/**
 * 產生 系統參數檔
 * @return	成功回傳 true，失敗 false
 */
	function makeSystemParamFile() {
		global $_Conn, $$_Conn;
		$strSQL = "select * from `bet_SystemParam`";
		if (!$RS = PMA_mysql_query($strSQL, $$_Conn)) {
			$GLOBALS["errormessage"] = "Find Param Data Error";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		$arrSysParam = array();
		while ($Rows = PMA_mysql_fetch_object($RS)) {
			$arrSysParam[$Rows -> ParamName] = $Rows -> ParamValue;
		}
		$strCon = serialize($arrSysParam);
		$strCon = addslashes($strCon);

		$strFile = __Common_Path."/SysParam.inc.php";
		$strCon = "<?php\n\$_KSysParam=unserialize(\"".$strCon."\");\n?".">";

		if(!writeFile($strFile, $strCon, "w"))return false;
		return true;
	}
//-------------------------------------------------------//
//Tool_lib End
}
?>